package com.pmsystem.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pmsystem.pojo.Community;
import com.pmsystem.pojo.CommunityShow;
import com.pmsystem.service.CommunityService;

/**
 * 
* <p>Title: CommunityController</p>  
* <p>Description:处理小区设施信息的控制器</p>  
* @author 树昌  
* @date 2019年1月22日
 */
@Controller
@RequestMapping("Information/")
public class CommunityController {
	
	@Autowired
	CommunityService communityService;
	
	
	/**
	 * 
	 * <p>Title: addCommunity</p>  
	 * <p>Description: </p>  
	 * @author 树昌
	 * @param community
	 * @return 
	 */
	@RequestMapping(value="addcommunity",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addCommunity(Community community) {
		
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		boolean row = communityService.addCommunity(community);
		if(row) {
			msg = "success";
		}else {
			msg =  "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: showCommunity</p>  
	 * <p>Description:查询所有的小区设施记录 </p>  
	 * @author 树昌
	 * @return 不做跳转
	 */
	@RequestMapping(value="showcommunity",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> showCommunity(String keyword, Integer startRow, Integer pageSize,Integer CID){
		
		Map<String, Object> map = new HashMap<>();
		List<CommunityShow> listcommunity = communityService.showCommunity(keyword,startRow,pageSize,CID);
		map.put("listcommunity", listcommunity);
		int size = listcommunity.size();
		map.put("size", size);
		return map;
	}
	
	/**
	 * 
	 * <p>Title: showCommunityById</p>  
	 * <p>Description:根据小区设施的ID查询一条设施的记录 </p>  
	 * @author 树昌
	 * @param id
	 * @return
	 */
	@RequestMapping(value="showCommunityById",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> showCommunityById(int id){
		
		Map<String, Object> map = new HashMap<>();
		Community community = communityService.showCommunityById(id);
		map.put("community", community);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: deleteCommunity</p>  
	 * <p>Description:删除一条小区设施的记录 </p>  
	 * @author 树昌
	 * @param community
	 * @return 
	 */
	@RequestMapping(value="deletecommunity",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteCommunity(Community community) {
		
		
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		boolean row = communityService.deleteCommunity(community);
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
		
	}
	
	
	/**
	 * 
	 * <p>Title: deleteAllCommunity</p>  
	 * <p>Description:批量删除社区设施的记录 </p>  
	 * @author 树昌
	 * @param id
	 * @return
	 */
	@RequestMapping(value="deleteAllCommunity",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteAllCommunity(int[] id){
		
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		List<Integer> listID = new ArrayList<>();
		for (int i : id) {
			listID.add(i);
		}
		
		boolean row = communityService.deleteAllCommunity(listID);
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	
	
	/**
	 * 
	 * <p>Title: updateCommunity</p>  
	 * <p>Description:修改一条小区设施的记录 </p>  
	 * @author 树昌
	 * @param community
	 * @return 
	 */
	@RequestMapping(value="updatecommunity",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateCommunity(Community community) {
		
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		boolean row = communityService.updateCommunity(community);
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
}
